<template>
  <div class="adminList-wrap">
    <el-button-group
      style="margin-top: 10px; margin-bottom: 30px; float: right"
    >
      <el-button
        type="primary"
        style="margin-right: 20px"
        :icon="Plus"
        @click="fnRegister"
      >
        注册管理员
      </el-button>
    </el-button-group>
    <el-input
      v-model="searchInput"
      style="width: 340px"
      size="large"
      placeholder="请输入搜索内容"
      :prefix-icon="Search"
      clearable
      @input="fnInput"
    />
    <el-table
      :data="datas"
      border
      style="width: 100%"
      :header-cell-style="{ background: '#f5f5f5', color: '#606266' }"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column label="用户名称" min-width="180" align="center">
        <template #default="scope">{{
          scope.row.user_name ? scope.row.user_name : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="昵称" min-width="160" align="center">
        <template #default="scope">{{
          scope.row.nick_name ? scope.row.nick_name : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="手机号" min-width="160" align="center">
        <template #default="scope">{{
          scope.row.mobile ? scope.row.mobile : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="邮箱" min-width="160" align="center">
        <template #default="scope">{{
          scope.row.email ? scope.row.email : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="登录有效期" min-width="100" align="center">
        <template #default="scope">{{
          scope.row.token_expired == 0
            ? '永不失效'
            : scope.row.token_expired + '分钟'
        }}</template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="220"
        fixed="right"
      >
        <template #default="scope">
          <el-button
            type="primary"
            size="small"
            @click="fnPermission(scope.row)"
          >
            授权API
          </el-button>
          <el-button type="success" size="small" @click="fnSetting(scope.row)">
            设置
          </el-button>
          <el-button type="warning" size="small" @click="fnModify(scope.row)">
            修改
          </el-button>
          <el-button type="danger" size="small" @click="fnDelete(scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      class="page-warp"
      background
      layout="total,prev, pager, next, sizes"
      v-model:page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      @current-change="fnCurrentChange"
      @size-change="fnSizeChange"
    />
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { postData } from '@/http/request.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Search } from '@element-plus/icons-vue'
let router = useRouter()

let datas = ref([])
let res_datas = ref([])
let searchInput = ref('')
let total = ref(0)
let currentPage = ref(1)   //当前第几页
let pageSize = ref(10)    //每页几个


//init
let getList = () => {
  postData(
    '/sys/admin/list',
    {
      page: 0,
    },
    {
      timeout: 120000,
    }
  )
    .then((res) => {
      if (res.code == 0) {
        total.value = res?.datas?.admins?.length || 0;
        res_datas.value = res?.datas?.admins || [];
        datas.value = res_datas.value.slice(0, pageSize.value);
      } else if (res.code == -56055) {
        return
      } else {
        ElMessage({
          type: 'error',
          message: 'error',
        })
      }
    })
    .catch((res) => {
      ElMessage({
        type: 'error',
        message: 'error',
      })
    })
}

//删除,当前行
let fnDelete = (dataRow) => {
  ElMessageBox.confirm('确认删除该用户?', '删除确认', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      postData('/sys/admin/delete', {
        user_wanted: dataRow.user_name,
      }).then((res) => {
        if (res.code == 0) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          currentPage.value = 1
          getList()
        } else {
          ElMessage({
            type: 'error',
            message: '删除失败',
          })
        }
      })
    })
    .catch(() => {})
}


let fnRegister = () => {
  router.push('/adminRegister')
}

let fnPermission = (data) => {
  router.push('/adminPermission?id=' + data.user_name)
}

let fnModify = (data) => {
  router.push('/adminModify?id=' + data.user_name)
}

let fnSetting = (data) => {
  router.push('/adminSetting?id=' + data.user_name)
}



let fnSizeChange = (data) => {
  pageSize.value = data
  currentPage.value = 1
  getList()
}


let fnCurrentChange = (data) => {
  let start = (data - 1) * pageSize.value
  currentPage.value = data
  datas.value = res_datas.value.slice(start, start + pageSize.value)
}

//search
let fnInput = (val) => {
  if (val) {
    // datas.value = datas.value.filter((item) => {
    //   return item.dev_id.includes(val)
    // })
    datas.value = datas.value.filter((item) => {
      return Object.keys(item).some((key) => {
        return String(item[key]).toLowerCase().indexOf(val) > -1
      })
    })
  } else {
   getList()
  }
  currentPage.value = 1
  total.value = datas.value.length
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.adminList-wrap {
  width: 100%;
  height: 100%;
  position: relative;
}
.search-wrap {
  display: flex;
  justify-content: flex-start;
}

.adminList-wrap >>> .el-table__header-wrapper {
  background: red;
}
.adminList-wrap >>> .el-table__header {
  background: red;
}
.page-warp {
  float: right;
  margin-top: 10px;
}
</style>
